// This servet is called by login.js or login.html
// This code is currently not working, there seems to be some problem with the string comparison

//package NetTrust;
import java.sql.*;import java.util.Date;
import java.io.*;
import java.util.*;
import java.util.HashMap;
import javax.servlet.http.*;
import javax.servlet.ServletException;
import javax.servlet.ServletConfig;
/*CheckNymServlet will check if the user nymname entered and their pwds match... thus we need to query the nymnamemap to find the nymid and then nymdetail to check if pwd entered is right..if right it is supposed to call nettrustfinalservlet1 with this nym*/
public class CheckNymServlet extends HttpServlet{
        //Initialize the servlet
        public void init(ServletConfig config)
        {         System.out.println("FriendsServlet: init()");
        }

       public void doGet(HttpServletRequest request,HttpServletResponse response)
              throws ServletException, IOException
      {
                String strPrevNym=null;               // set the response type
                response.setContentType("text/xml; charset=UTF-8");
                response.setHeader("Cache-Control","no-cache");
                String strNym=request.getParameter("nym");
                String strpwd = request.getParameter("pwd");
                int success=this.CheckNym(strNym,strpwd);
            // obtain the writer to write the response
                PrintWriter out = response.getWriter();
                out.println("<?xml version=\"1.0\"?>");
                out.println("<Success>");
                out.println("<valid>");
                out.println(success);
                out.println("</valid>");
                out.println("</Success>"); 
   // if this servlet works, we need to figure out a way to allow this servlet to pass the nym to nt.js?? that is a little doubtful!

     }

        /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
      // Connect()
  // Connects to the database
 // return value of type Connection
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
      public static Connection Connect()
        {
                Connection conn = null;
                try               
                   {
                       //Defines the Query statements
                      //ResultSet rsBuddy,rsInternal,rsNymID;
                     //Connect to the database
                      String userName = "nettrust";
                      String password = "nettrust06162006";
                      String url= "jdbc:mysql://localhost/NetTrust";
                      Class.forName ("com.mysql.jdbc.Driver").newInstance ();

                     conn = DriverManager.getConnection (url, userName, password);
                     System.out.println ("Database connection established");
                  }
                catch( Exception e )
                {
                  // Do nothing
                  System.out.println("Connection failed");
                }

                return( conn );
        }
 
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 // end of function Connect()
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

        public static int CheckNym(String strNym,String strPwd)
        {
              Connection Conn=Connect();
              Statement stCheckNym;
              ResultSet getNymID;
              String strNymid;
              ResultSet chkPwd;
              String pass;
   
               try{
                       stCreateNym = Conn.createStatement();
                        try
                                {
                                     getNymID = stCheckNym.executeQuery("select NymID from nymnamemap where NymName = '"+strNym+"'");
                                     while(getNymID.next())
                                       {
                                         strNymid = getNymID.getString("NymID");
                                         chkPwd = stCheckNym.executeQuery("select Password from nymdetail where NymID = '"+strNymid+"'");
                                         
                                            while(chkPwd.next())  
                                             {
                                                pass = chkPwd.getString("Password"); 
                                                if(strPwd.equals(pass))   
                                                  return 1; //this is success
                                             }  
                                       }
                                               return 0;
                                }
                  
                       catch(Exception e)                                
                               {
                                        System.out.println (e);
                                        return 0;
                                }
                  }
                    
               catch(Exception e)
                        {
                                System.out.println (e);
                                return 0;
                        }
        }

        public void destroy() {
                System.out.println("CheckNymServlet: destroy()");
      }
}
                                                                                                                                                                                   104,1         Bot


